using System;
using System.IO;
using System.Text;

namespace Microsoft.EPM.TSAutoStatus
{
    class AppLog
    {
        #region Properties

        private StreamWriter swLog;
        private string fileName;

        /// <summary>
        /// Stream writer
        /// </summary>
        public StreamWriter SwLog
        {
            get { return swLog; }
        }

        /// <summary>
        /// File name
        /// </summary>
        public String FileName
        {
            get { return fileName; }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Create log file
        /// </summary>
        /// <param name="prefixName">Prefix name</param>
        /// <param name="pwa">PWA instance name</param>
        /// <param name="logFolder">Folder location</param>
        /// <param name="logFileEveryHour">Create a log file every hour</param>
        public AppLog(string prefixName, string logFolder, bool logFileEveryHour)
        {
            fileName = string.Format("{0}TimesheetEvent_{1}.log", prefixName, GeneralHelper.FormatTimeLog(true));

            if (!logFolder.EndsWith(@"\")) logFolder += @"\";
            string path = logFolder + fileName;
            try
            {
                if (!logFileEveryHour)
                    swLog = new StreamWriter(path);
                else
                {
                    // Check to see if file exists
                    if (File.Exists(path))
                        swLog = new StreamWriter(path, true);
                    else
                        swLog = new StreamWriter(path);
                }
            }
            catch (Exception)
            {
                System.Threading.Thread.Sleep(1000);
                swLog = new StreamWriter(Environment.CurrentDirectory + "\\" + string.Format("{0}TimesheetEvent_{1}.log", prefixName, Guid.NewGuid().ToString()));
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Write entry to log file
        /// </summary>
        /// <param name="toWrite">entry to write</param>
        public void WriteLine(string toWrite)
        {
            WriteLine(toWrite, true);
        }

        /// <summary>
        /// Write entry to log file
        /// </summary>
        /// <param name="toWrite">entry to write</param>
        /// <param name="dateTime">write date time?</param>
        public void WriteLine(string toWrite, bool dateTime)
        {
            if (dateTime)
                swLog.WriteLine(string.Format("{0}: {1}", DateTime.Now.ToLongTimeString(), toWrite));
            else
                swLog.WriteLine(toWrite);
        }

        /// <summary>
        /// Write entry to log file
        /// </summary>
        /// <param name="sw">stream writer</param>
        /// <param name="toWrite">entry to write</param>
        public static void WriteLine(StreamWriter sw, string toWrite)
        {
            sw.WriteLine(DateTime.Now.ToLongTimeString() + " : " + toWrite);
        }

        /// <summary>
        /// Close stram writer
        /// </summary>
        public void Close()
        {
            swLog.Close();
        }

        #endregion
    }
}
